import 'dart:math';

import 'IdentityUtils.dart';

///字符串
class RuleUtils {
  static RuleUtils instance = RuleUtils();

  ///验证字符串格式是否正确
  String? checkStringRule(RuleType ruleType, String value, bool? noNull) {
    if (ruleType == RuleType.phone) {
      if (value.isEmpty) return noNull! ? "手机号码不能为空" : null;
      if (value.length != 11) return "手机号码长度为11位";
      bool sucess =
          RegExp('^((13[0-9])|(15[^4])|(166)|(17[0-8])|(18[0-9])|(19[1-9])|(147,145))\\d{8}\$').hasMatch(value);
      if (!sucess) return "手机号码格式错误";
      return null;
    }
    if (ruleType == RuleType.email) {
      if (value.isEmpty) return noNull! ? "邮箱不能为空" : null;
      bool sucess = RegExp("^\\w+([-+.]\\w+)*@\\w+([-.]\\w+)*\\.\\w+([-.]\\w+)*\$").hasMatch(value);
      if (!sucess) return "邮箱格式错误";
      return null;
    }
    if (ruleType == RuleType.identity) {
      if (value.isEmpty) return noNull! ? "身份证号码不能为空" : null;
      String? tip = IdentityUtils.verifyCardId(value);
 return tip;
      return null;
    }

    if (ruleType == RuleType.chinese) {
      bool sucess = RegExp("[\u4e00-\u9fa5]").hasMatch(value);
      if (sucess) return "不能包含中文";
      return null;
    }

    if (value.isEmpty) {
      return noNull! ? "输入不能为空" : null;
    } else {
      return null;
    }
  }
}

///字符串规格校验
enum RuleType {
  none, //无需进行规则校验
  phone, //手机号码
  email, //邮箱
  identity, //身份证号码
  chinese, //中文
}
